package com.booth.base.controllers;

import java.util.List;

import com.booth.base.model.DataDict;
import com.booth.base.services.IDataDictService;
import com.booth.base.services.impl.DataDictService;
import com.booth.base.utils.ResultStatus;

public class DataDictController extends BaseController
{
  private IDataDictService service = new DataDictService();

  public void index()
  {
    render("data_dict.jsp");
  }

  public void au_page() {
    int type = getParaToInt("type", Integer.valueOf(0)).intValue();
    int id = getParaToInt("id", Integer.valueOf(0)).intValue();
    DataDict dataDict = null;
    if (id > 0)
    {
      dataDict = (DataDict)DataDict.dao.findById(Integer.valueOf(id));
      if (dataDict == null)
        type = 0;
    }
    if (type == 1)
    {
      if (dataDict != null)
      {
        if (dataDict.getInt("dat_id") != null)
        {
          dataDict = (DataDict)DataDict.dao.findById(dataDict.getInt("dat_id"));
        }
        setAttr("datId", dataDict.getInt("id"));
        setAttr("pname", dataDict.getStr("name"));
      }
      else
      {
        type = 0;
      }
    }
    else if (type == 2)
    {
      if (dataDict != null)
      {
        setAttr("dataDict", dataDict);
        if (dataDict.getInt("dat_id") != null)
        {
          DataDict p = (DataDict)DataDict.dao.findById(dataDict.getInt("dat_id"));
          setAttr("datId", p.getInt("id"));
          setAttr("pname", p.getStr("name"));
          type = 2;
        }
        else
        {
          type = 3;
        }
      }
    }
    setAttr("type", Integer.valueOf(type));
    render("data_dict_au.jsp");
  }

  public void list()
  {
    List lst = this.service.showDataDictTree(null);
    setAttr("rows", lst);
    renderResult(ResultStatus.SUCCESS);
  }

  public void save()
  {
    boolean ret = false;
    DataDict dataDict = (DataDict)getModel(DataDict.class);
    if ((dataDict.getInt("id") != null) && (dataDict.getInt("id").intValue() > 0))
    {
      ret = dataDict.update();
      if (!ret)
      {
        renderResult(ResultStatus.UPDATE_FAILURE);
      }
    }
    else
    {
      ret = dataDict.save();
      if (!ret)
      {
        renderResult(ResultStatus.ADD_FAILURL);
      }
    }
    if (ret)
    {
      renderResult(ResultStatus.SUCCESS);
    }
  }

  public void delete()
  {
    int id = getParaToInt("id", Integer.valueOf(0)).intValue();
    if (id > 0)
    {
      int subs = DataDict.dao.subCount(Integer.valueOf(id));
      if (subs > 0)
      {
        renderResult(Integer.valueOf(100), "此数据已经被使用，不能删除！");
      }
      else
      {
        boolean ret = DataDict.dao.deleteById(Integer.valueOf(id));
        if (ret)
        {
          renderResult(ResultStatus.SUCCESS);
        }
        else
        {
          renderResult(ResultStatus.DELETE_FAILURE);
        }
      }
    }
    else
    {
      renderResult(ResultStatus.DELETE_NO_DATA);
    }
  }

  public void showDataDict()
  {
    String code = getPara("code");
    boolean flag = getParaToBoolean("flag", Boolean.valueOf(false)).booleanValue();
    List lst = this.service.showDataDictByCode(code);
    if (flag)
    {
      DataDict dt = new DataDict();
      dt.set("name", "--无--");
      lst.add(0, dt);
    }
    renderJson(lst);
  }
}